<?php

namespace app\common\model\banxi\learn;

use app\common\enums\AccountTypeEnum;
use app\common\enums\RangeLevelEnum;
use app\common\model\banxi\core\CoreVocabularySetting;
use app\common\model\banxi\trainer\Trainer;
use app\common\model\common\BaseModel;
use app\common\service\AccountService;
use think\model\concern\SoftDelete;

/**
 * 词汇量测试记录
 */
class LearnVocabularyRecord extends BaseModel
{
    use SoftDelete;

    protected $name = 'banxi_learn_vocabulary_record';

    protected $autoWriteTimestamp = true;

    protected $deleteTime = 'del';

    protected $defaultSoftDelete = 0;

    protected $append = [
        'accountTypeCn'
    ];

    protected $type = [
        'id' => 'string',
        'del' => 'string',
        'skRecordId' => 'string',
        'tutorTrainerId' => 'string',
        'trainerId' => 'string',
        'studentId' => 'string',
        'level' => 'string',
        'isSubmit' => 'boolean',
        'createTime' => 'timestamp:Y-m-d H:i:s',
        'updateTime' => 'timestamp:Y-m-d H:i:s',
    ];


    //-----------------------------------------------------------------------------------
    //｜                                   获取器                                       ｜
    //-----------------------------------------------------------------------------------

    public function getAccountTypeCnAttr($value, $data)
    {
        $value = $value ?: ($data['accountType'] ?? '');
        return (new AccountTypeEnum())->getDescription($value) ?? '';
    }


    //-----------------------------------------------------------------------------------
    //｜                                   处理方法                                       ｜
    //-----------------------------------------------------------------------------------


    public function getDetail($recordId)
    {
        $detail = $this->where('id', $recordId)->find();
        if (!$detail) {
            getResponseException('记录不存在', [],1,'ERROR');
        }
        $accountId = $detail['accountId'];
        $accountType = $detail['accountType'];
        $accountService = new AccountService();
        $detail->accountInfo = $accountService->getAccountInfo($accountId, $accountType);
        $coreVocabularySettingModel = new  CoreVocabularySetting();
        $settingList = $coreVocabularySettingModel->listBeginner();
        $detail->settingList = $settingList;
        $detail->settingInfo = $coreVocabularySettingModel->where(['id'=>$detail['level']])->find();
        $detail->highSchoolSettingInfo = $coreVocabularySettingModel->getByRangeLevelAndName(RangeLevelEnum::E3, '高中');
        return $detail;
    }

    //-----------------------------------------------------------------------------------
    //｜                                   模型关联                                       ｜
    //-----------------------------------------------------------------------------------

    public function trainerInfo()
    {
        return $this->hasOne(Trainer::class, 'id', 'trainerId');
    }

    public function studentInfo()
    {
        return $this->hasOne(Trainer::class, 'id', 'studentId');
    }

    public function settingInfo()
    {
        return $this->hasOne(CoreVocabularySetting::class, 'id', 'level');
    }

}